public class DirectionTile {
	private DirectionTile parent;
	
	private boolean visited=false;
	private char myInstruction;
	
	private static final char NIL   = 'x';

	public DirectionTile() {
	}
	
	public boolean isDestination() {
		return ((parent == null));
	}
	
	public void markAsVisited() {
		visited=true;
	}
	
	public void addParent(DirectionTile parent, char instruction) {
		this.parent = parent;
		myInstruction = instruction;
	}
	
	public char getInstruction() {
		return myInstruction;
	}
	
	/**
	 * Removes the instruction and the parent of the direction tile.
	 * This method DOES NOT remove any instruction lists
	 */
	public void flush() {
		parent = null;
		myInstruction = NIL;
		visited = false;
	}
	
	public boolean isVisited() {
		return ((parent!=null)||(visited));
	}
	
	public DirectionTile getParent() {
		return parent;
	}
	
	
	/**
	 * Follow the "arrows" and collect instructions. When you reach a destination (i.e. a parent-less tile
	 * to which all arrows point, save the final collection of instructions at the initial DirectionTile.
	 * @return The collections of instruction from the current DirectionTile, to the unique reachable Destination tile
	 */
	public StringBuffer makeInstructionList(char identifier) {
		StringBuffer newInstructionList = new StringBuffer();
		
		if (identifier != NIL) 
			newInstructionList.append(identifier);
		
		
		DirectionTile currentDirectionTile = this;
		int c=0;
		while(true) {
			//System.out.print(currentDirectionTile.getInstruction() + " ");
			c++;

			
			DirectionTile nextTile = currentDirectionTile.getParent();
			
			if (nextTile!=null) {
				char newInstruction = currentDirectionTile.getInstruction();
				//newInstructionList.append(newInstruction);
				newInstructionList.insert(0, newInstruction);
				currentDirectionTile = nextTile;
			} else {
				//this.addInstructionList(newInstructionList, identifier);
				//System.out.println(newInstructionList.toString());
				break;
			}			
		}

		return newInstructionList;
	}
}
